package com.six666.medicaldevicetestingsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.six666.medicaldevicetestingsystem.entity.Pair;
import com.six666.medicaldevicetestingsystem.entity.ProjectTest;
import com.six666.medicaldevicetestingsystem.mapper.ProjectTest_Mapper;
import com.six666.medicaldevicetestingsystem.service.ProjectTest_Service;
import com.six666.medicaldevicetestingsystem.service.TestOffiers_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProjectTest_Service_Impl extends ServiceImpl<ProjectTest_Mapper, ProjectTest> implements ProjectTest_Service{
    @Autowired
    TestOffiers_Service testOffiers_Service;
    @Override
    public Boolean create_compare_plan(ProjectTest projectTest,int accountid){

        QueryWrapper<ProjectTest> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("ProjectID",projectTest.getProjectid());
        queryWrapper2.eq("State","Ongoing");
        int size=list(queryWrapper2).size();
        if(size==0){
            QueryWrapper<ProjectTest> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("ProjectID",projectTest.getProjectid());
            queryWrapper.eq("State","Finish");
            int epoch=list(queryWrapper).size()+1;
            projectTest.setEpoch(epoch);
            save(projectTest);
        }


        ProjectTest projectTest1=getOne(queryWrapper2);
        int testid=projectTest1.getTestid();
        return testOffiers_Service.insert_testoffiers(testid, accountid);

    }
    @Override
    public List<ProjectTest> get_my_test(List<Integer> test_id){
        List<ProjectTest> res=new ArrayList<>();
        for (Integer integer : test_id) {
            QueryWrapper<ProjectTest> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("TestID", integer);
            res.add(getOne(queryWrapper));

        }
        return res;
    }

    @Override
    public Pair<List<Integer>, Date> get_project_test_id(Integer project_id) {
        QueryWrapper<ProjectTest> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ProjectID",project_id);
        queryWrapper.eq("State","Ongoing");
        List<ProjectTest> test_list=list(queryWrapper);

        Pair<List<Integer>,Date> res=new Pair<>(new ArrayList<>(),new Date());
        for (ProjectTest projectTest : test_list) {
            res.first.add(projectTest.getTestid());
            res.second = projectTest.getTesttime();
        }

        return res;
    }

    @Override
    public boolean finish_by_project_id(int projectId) {
        UpdateWrapper<ProjectTest> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ProjectID", projectId).eq("State","Ongoing").set("State", "Finish");
        return update(updateWrapper);
    }

    @Override
    public boolean finish_by_test_id(int testId) {
        UpdateWrapper<ProjectTest> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("TestID", testId).set("State", "Finish");
        return update(updateWrapper);
    }

    @Override
    public List<ProjectTest> get_all_test() {
        QueryWrapper<ProjectTest> queryWrapper = new QueryWrapper<>();
        return list(queryWrapper);
    }


}
